
#include "BScheduler.h"
using namespace blueberry;
//Comment

BClient::BClient(const char * _zIP, UInt _uiPort, UInt _uiClientID, bool _bHB, BClientCallback * _pCCB, BScheduler * _pEventMan, BServer * _pServer) :
	BItem(E_EVT_CLIENT)
{
	ui_ClientID = _uiClientID;
	p_ClientCB = _pCCB;
	p_Scheduler = _pEventMan;
	ui_ServerPort = _uiPort;
	p_Server = _pServer;
	strncpy(z_ServerIP, _zIP, MAX_IP_ADDRESS_LEN);
	b_HB = _bHB;
	b_IsBuffered = false;
	b_IsDataLoggingEnabled = false;
	p_Data = NULL;
}

BClient::~BClient()
{
	p_Scheduler->AddToDeletedClientSet(ui_ClientID);
}
bool BClient::Send(const char * _zData, int _iLen)
{
	m_SendBuffer.Append(_zData, _iLen);
	p_Scheduler->SendToClient(ui_ClientID);
	return !b_IsBuffered;
}
void BClient::SendBufferedData()
{
	p_Scheduler->SendToClient(ui_ClientID);
}
void BClient::Detach()
{
	p_Scheduler->Detach(this);
}

void BClient::SetSocketSendBufferSize(UInt _uiSize)
{
	unsigned int uiMask = _uiSize;
	setsockopt(ui_ClientID, SOL_SOCKET, SO_SNDBUF, &uiMask, sizeof(unsigned int));
}
void BClient::SetSocketRecvBufferSize(UInt _uiSize)
{
	unsigned int uiMask = _uiSize;
	setsockopt(ui_ClientID, SOL_SOCKET, SO_RCVBUF, &uiMask, sizeof(unsigned int));
}

void BClientCallback::OnData(BServer * _pServer, BClient * _pClient, char * _zData, int _iLen)
{
	_pServer = _pServer;
	_pClient = _pClient;
	_zData = _zData;
	_iLen = _iLen;
}
void BClientCallback::OnData(BClient * _pClient, char * _zData, int _iLen)
{
	_pClient = _pClient;
	_zData = _zData;
	_iLen = _iLen;
}

void BClientCallback::OnDisconnect(BServer * _pServer, BClient * _pClient, BErr _Err)
{
	_pServer = _pServer;
	_pClient = _pClient;
	_Err = _Err;
}
void BClientCallback::OnDisconnect(BClient * _pClient, BErr _Err)
{
	_pClient = _pClient;
	_Err = _Err;
}

void BClientCallback::OnReadyToSend(BClient * _pClient)
{
	_pClient = _pClient;
}
